package model

import (
	"gorm.io/gorm"
	"homepage_srv/homepage"
	"smart-health-platforms/config"
	"time"
)

// 商品表
type Good struct {
	Id            uint64         `gorm:"column:id;type:bigint UNSIGNED;comment:商品id;primaryKey;not null;" json:"id"`                          // 商品id
	ShopId        int32          `gorm:"column:shop_id;type:int;comment:商铺id;not null;" json:"shop_id"`                                       // 商铺id
	BrandId       uint32         `gorm:"column:brand_id;type:int UNSIGNED;comment:品牌id;not null;default:0;" json:"brand_id"`                  // 品牌id
	MainImage     string         `gorm:"column:main_image;type:varchar(512);comment:商品主图URL;not null;" json:"main_image"`                   // 商品主图URL
	SliderImageId uint32         `gorm:"column:slider_image_id;type:int UNSIGNED;comment:轮播图id;not null;default:0;" json:"slider_image_id"`  // 轮播图id
	StoreName     string         `gorm:"column:store_name;type:varchar(128);comment:商品名称;not null;" json:"store_name"`                      // 商品名称
	StoreInfo     string         `gorm:"column:store_info;type:varchar(256);comment:商品简介;not null;" json:"store_info"`                      // 商品简介
	CateId        uint32         `gorm:"column:cate_id;type:int UNSIGNED;comment:分类id;not null;default:0;" json:"cate_id"`                    // 分类id
	Price         float64        `gorm:"column:price;type:decimal(8, 2);comment:商品价格;not null;default:0.00;" json:"price"`                  // 商品价格
	Stock         uint32         `gorm:"column:stock;type:int UNSIGNED;comment:库存;not null;default:0;" json:"stock"`                          // 库存
	Hot           uint32         `gorm:"column:hot;type:int UNSIGNED;comment:商品热度;not null;default:0;" json:"hot"`                          // 商品热度
	Sales         uint32         `gorm:"column:sales;type:int UNSIGNED;comment:销量;not null;default:0;" json:"sales"`                          // 销量
	IsShow        uint8          `gorm:"column:is_show;type:tinyint UNSIGNED;comment:状态（0：未上架，1：上架）;not null;default:1;" json:"is_show"` // 状态（0：未上架，1：上架）
	CreatedAt     time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt     time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt     gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

// 关联数据库
func (g *Good) TableName() string {
	return "good"
}

// 商品添加
func (g *Good) Create() error {
	return config.DB.Create(g).Error
}

// 商品详情
func (g *Good) Detail() error {
	return config.DB.Where("id = ?", g.Id).Limit(1).Find(&g).Error
}

// 商品热度自增
func (g *Good) GoodHot() error {
	return config.DB.Model(&g).Where("id = ?", g.Id).Update("hot", gorm.Expr("hot + 1")).Error
}

// 商品搜索
func (g *Good) ListGood(Name string) (albumsList []*homepage.GoodSearch, err error) {
	err = config.DB.Model(&Good{}).
		Select("good.*, brands.brand_name,brands.brand_description").
		Joins("join brands on good.brand_id = brands.id").
		Where("good.store_name like ? || brands.brand_name like ?", "%"+Name+"%", "%"+Name+"%").
		Find(&albumsList).Error
	return
}

// 商品列表
func (g *Good) GoodList(id int) (albumsList []*homepage.GoodList, err error) {
	err = config.DB.Model(&Good{}).
		Select("good.*, brands.brand_name,brands.brand_description").
		Joins("join brands on good.brand_id = brands.id").
		Where("good.cate_id = ?", id).
		Order("good.sales desc").
		Find(&albumsList).Error
	return
}
func (g *Good) GetGoodId(id int) error { //订单查询商品
	return config.DB.Where("id = ?", id).Limit(1).Find(&g).Error
}
func (g *Good) UpdateGoodStock() error { //订单修改库存
	return config.DB.Where("id = ?", g.Id).Update("stock", g.Stock).Error

}

// 商品推荐,根据热度
func (g *Good) GoodRecomment(cateId uint64) (goodList []*homepage.Recomment, err error) {
	err = config.DB.Model(&Good{}).
		Select("good.*,brands.brand_name,brands.brand_description").
		Joins("join brands on good.brand_id = brands.id").
		Where("good.cate_id = ?", cateId).
		Order("good.hot desc").Limit(5).
		Find(&goodList).Error
	return
}

// 根据商品查询所属店铺信息
func (g *Good) GoodShop(goodId uint64) (goodShop *Shop, err error) {
	err = config.DB.Model(&Good{}).
		Select("shop.*").
		Joins("join shop on good.shop_id = shop.id").
		Where("good.id = ?", goodId).Limit(1).
		Find(&goodShop).Error
	return
}

// 商品热词搜索
func (g *Good) ListHotGood(Name string) (albumsList []*homepage.HotWordSearch, err error) {
	err = config.DB.Model(&Good{}).
		Select("good.*, brands.brand_name,brands.brand_description").
		Joins("join brands on good.brand_id = brands.id").
		Where("good.store_name like ? || brands.brand_name like ?", "%"+Name+"%", "%"+Name+"%").
		Find(&albumsList).Error
	return

}
